import requests


headers = {
    'User-Agent': "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:78.0) Gecko/20100101 Firefox/78.0"
}

session = requests.session()  # 保存登录状态
session.headers = headers


def get_html(url):  # 访问网页
    html = session.get(url)

    if html.status_code == 200:  # 状态码

        c = parse_html(html.json())
        return c
    else:
        print('访问网页错误')


def parse_html(html):  # 解析网页

    data = html['data']
    c = []

    for i in data:
        try:
            img = i['middleURL']
            c.append(img)
            print(img)
        except:
            pass
    return c


def start(name, nums):
    # if __name__ == '__main__':
    # name = input("图库内容:")
    # nums = int(input("数量:"))
    for i in range(1, nums):
        url = 'https://image.baidu.com/search/acjson?tn=resultjson_com'\
            '&ipn=rj&ct=201326592&is=&fp=result&queryWord={}'\
            '&cl=2&lm=-1&ie=utf-8&oe=utf-8&adpicid=&st=-1&z=&ic='\
            '&hd=&latest=&copyright=&word={}&s=&se=&tab=&width='\
            '&height=&face=0&istype=2&qc=&nc=1&fr=&expermode='\
            '&force=&pn={}&rn=30'.format(name, name, i)

        c = get_html(url)
        return c


